package com.warrantyhotel.android.util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.ui.NearbyStoresDetail;
import com.warrantyhotel.android.widget.BalloonView;
import com.warrantyhotel.android.widget.StoreItem;
import com.warrantyhotel.android.widget.StorePinMapOverlay;

public class StorePinch {

	private static StorePinch instance = null;

	private int index;
	private ArrayList<StoreItem> mOverlays;
	private MapView mapView;
	private Context context;
	private OnRedirectToListStores onRedirectToListStores;
	private GeoPoint geoPoint;
	private BalloonView balloonView;

	private StorePinch(int index, ArrayList<StoreItem> mOverlays, MapView mapView, Context context, OnRedirectToListStores onRedirectToListStores,
			GeoPoint point) {

		this.index = index;
		this.mOverlays = mOverlays;
		this.mapView = mapView;
		this.context = context;
		this.geoPoint = point;
		this.onRedirectToListStores = onRedirectToListStores;

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				handler.sendEmptyMessage(0);
			}
		}, 100);
	}

	public static StorePinch getInstance(int index, ArrayList<StoreItem> mOverlays, MapView mapView, Context context,
			OnRedirectToListStores onRedirectToListStores, GeoPoint point) {
		if (instance == null) {
			instance = new StorePinch(index, mOverlays, mapView, context, onRedirectToListStores, point);
		}
		return instance;
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (StorePinMapOverlay.count == 1) {
				String id = mOverlays.get(index).getId();
				String banner = mOverlays.get(index).getBanner();
				if (id != null) {
					mapView.getController().animateTo(mOverlays.get(index).getPoint());
					final Intent intent = new Intent(context, NearbyStoresDetail.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra(Keys._retailStoreId, id);
					intent.putExtra(Keys.banner, banner);
					context.startActivity(intent);
				} else {
					((Activity) context).finish();
				}
			} else {
				// onRedirectToListStores.
				if (mapView.getZoomLevel() >= mapView.getMaxZoomLevel()) {
					onRedirectToListStores.redirectToList();
				} else {
					mapView.removeAllViews();
					balloonView = new BalloonView(context, StorePinMapOverlay.count);
					MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, geoPoint,
							MapView.LayoutParams.BOTTOM_CENTER);
					params.mode = MapView.LayoutParams.MODE_MAP;
					mapView.addView(balloonView, params);

					mapView.getController().setZoom(mapView.getZoomLevel() + 2);
					mapView.getController().animateTo(mOverlays.get(index).getPoint());
				}
			}

			StorePinMapOverlay.count = 0;
			instance = null;

		}
	};

}
